#!/usr/bin/python

#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
#       


import board
from engines import *

def human_make_move():
    while True:
        try:
            coord_str = raw_input("Player " + board.colours[pos.whose_turn]
                 + ": Please choose your move! ")
            coord = [int(c) for c in coord_str]
            pos.move(board.convert(coord))
            break
        except (ValueError, IndexError):
            print "Please enter a", board.dim, "digit number!"
        except board.IllegalMove as why:
            print "Illegal Move!", why

print "4D TicTacToe Alpha Version by DadB"
print """ Press....
1 for player against computer,
2 for two players,
3 for computer engine 1 against engine 2,
4 for computer engine 2 against engine 1."""
mode = int(raw_input("? "))
pos = board.Position()
while pos.are_squares_empty():
    if mode == 1:
        if pos.whose_turn == 1:
            human_make_move()
        else:
            computer_2_move(pos)
            board.print_board(pos)
            print pos.count_rows(1) , "rows for X"
            print pos.count_rows(-1), "rows for O"
    elif mode == 2:
        human_make_move()
        board.print_board(pos)
        print board.count_rows(1) , "rows for X"
        print board.count_rows(-1), "rows for O"
    elif mode == 3:
        if pos.whose_turn == 1:
            computer_1_move(pos)
        else:
            computer_2_move(pos)
    elif mode == 4:
        if pos.whose_turn == 1:
            computer_2_move(pos)
        else:
            computer_1_move(pos)
    else:
        raise ValueError
print "The board is full -> the game is over."
print pos.count_rows(1) , "rows for X"
print pos.count_rows(-1), "rows for O"
